package com.ssyu.examination.student.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.exam.entity.ExamPlanEntity;
import com.ssyu.examination.student.entity.AssistMessageEntity;
import com.ssyu.examination.student.service.StudentService;

@WebServlet("/StudentServlet")
public class StudentServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	private static final StudentService studentService = new StudentService();

	public StudentServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("StuExamList".equals(command)) {
			StuExamList(request, response);
		} else if ("StuMessage".equals(command)) {
			stuMessage(request, response);
		} else {
			throw new IllegalStateException("请求无效============！");
		}
	}

	private void StuExamList(HttpServletRequest request,
			HttpServletResponse response) {

		int id = App.getOperateId(request, response);
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		PageModel<ExamPlanEntity> stuExamList = studentService.stuListExam(
				page, rows, id);
		if (stuExamList != null) {
			print(response, FastjsonUtil.toJsonWithDateFormat(stuExamList,
					"yyyy-MM-dd HH:mm:ss"));
		}
		// HttpSession session = request.getSession();
		// Enumeration<?> enumeration = session.getAttributeNames();
		// // 遍历enumeration中的
		// while (enumeration.hasMoreElements()) {
		// // 获取session键值
		// String name = enumeration.nextElement().toString();
		// // 根据键值取session中的值
		// Object value = session.getAttribute(name);
		// // 打印结果
		// System.out.println("<B>" + name + "</B>=" + value + "<br>/n");
		// }
	}

	private void stuMessage(HttpServletRequest request,
			HttpServletResponse response) {
		int id = App.getOperateId(request, response);
		List<AssistMessageEntity> stuMessage = studentService.stuMessage(id);
		if (stuMessage != null) {
			print(response, FastjsonUtil.toJson(stuMessage));
		}
	}
}
